from PySide2.QtCore import QEvent
from PySide2.QtGui import Qt
from PySide2.QtWidgets import QTreeWidget,QTreeWidgetItem,QHeaderView


class TreeWidget(QTreeWidget):
    def __init__(self, parent = ...):
        super().__init__(parent)
        self.parent = parent
        self.setStyleSheet(u"QTreeWidget { border:1px solid rgb(100, 100, 100);}")


        self.setColumnCount(2)
        self.setHeaderLabels(['文件','状态'])
        self.setColumnWidth(0,260)
        self.setColumnWidth(1,60)
        # self.headerItem().setTextAlignment(0,Qt.AlignCenter)
        self.headerItem().setTextAlignment(1,Qt.AlignCenter)
        '''
        QHeaderView.Fixed：列的大小是固定的，不能调整。
        QHeaderView.ResizeToContents：列的大小会根据内容自动调整。
        QHeaderView.Stretch：列的大小会自动拉伸以填充可用空间。
        '''
        # 固定列宽
        # self.header().setSectionResizeMode(0, QHeaderView.Fixed)
        self.header().setSectionResizeMode(1, QHeaderView.Fixed)
        # 设置表头样式
        self.header().setStyleSheet("QHeaderView::section { background-color: gray; color: white; font-weight: bold; }")

        # 存储父类的鼠标释放事件
        self.mouseReleaseEvent_parent = self.parent.mouseReleaseEvent

        self.viewport().installEventFilter(self)


        self.__initTips()

    def eventFilter(self, watched, event):
        '''
            不用这个,拖拽文件到treewidget上时,鼠标指针会一直是 <-> 这样的,除非鼠标重新进入Widget区域
        '''
        if watched == self.viewport() and event.type() == QEvent.Enter:
            self.mouseReleaseEvent_parent(event)
        return super().eventFilter(watched, event)


    def __initTips(self):
        items = []
        items.append(QTreeWidgetItem(['1.请将文件或目录拖拽进来','']))
        items.append(QTreeWidgetItem(['2.点击检测','']))
        items.append(QTreeWidgetItem(['3.点击修复','']))
        items.append(QTreeWidgetItem(['4.再次点击检测,确认结果','']))
    
        self.addTopLevelItems(items)
